home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / c / echo.c < prev    next >
C/C++ Source or Header  |  1996-09-13  |  2KB  |  106 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: echo.c,v 1.4 1996/09/13 17:52:10 digulla Exp $
  4.     $Log: echo.c,v $
  5.     Revision 1.4  1996/09/13 17:52:10  digulla
  6.     Use IPTR
  7.  
  8.     Revision 1.3  1996/08/13 15:34:04  digulla
  9.     #include <exec/execbase.h> was missing
  10.  
  11.     Revision 1.2  1996/08/01 17:40:44  digulla
  12.     Added standard header for all files
  13.  
  14.     Desc:
  15.     Lang:
  16. */
  17. #include <exec/execbase.h>
  18. #include <exec/libraries.h>
  19. #include <clib/exec_protos.h>
  20. #include <dos/dos.h>
  21. #include <clib/dos_protos.h>
  22.  
  23. CALLENTRY /* Before the first symbol */
  24.  
  25. struct ExecBase *SysBase;
  26. struct DosLibrary *DOSBase;
  27.  
  28. static LONG tinymain(void);
  29.  
  30. LONG entry(struct ExecBase *sysbase)
  31. {
  32.     LONG error=RETURN_FAIL;
  33.     SysBase=sysbase;
  34.     DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",39);
  35.     if(DOSBase!=NULL)
  36.     {
  37.     error=tinymain();
  38.     CloseLibrary((struct Library *)DOSBase);
  39.     }
  40.     return error;
  41. }
  42.  
  43. static LONG tinymain(void)
  44. {
  45.     IPTR args[5]={ 0, 0, 0, 0, 0 };
  46.     struct RDArgs *rda;
  47.     STRPTR *a, b;
  48.     ULONG l, max=~0ul;
  49.     BPTR out=Output();
  50.     LONG error=0;
  51. #define ERROR(a) { error=a; goto end; }
  52.  
  53.     rda=ReadArgs("/M,NOLINE/S,FIRST/K/N,LEN/K/N,TO/K",args,NULL);
  54.     if(rda==NULL)
  55.     ERROR(RETURN_FAIL);
  56.  
  57.     if(args[3])
  58.     max=*(ULONG *)args[3];
  59.  
  60.     if(args[4])
  61.     {
  62.     out=Open((STRPTR)args[4],MODE_NEWFILE);
  63.     if(!out)
  64.         ERROR(RETURN_ERROR);
  65.     }
  66.  
  67.     a=(STRPTR *)args[0];
  68.     while(*a!=NULL)
  69.     {
  70.     b=*a;
  71.     while(*b++)
  72.         ;
  73.     l=b-*a-1;
  74.     b=*a;
  75.     if(args[2]&&*(ULONG *)args[2])
  76.     {
  77.         if(*(ULONG *)args[2]-1<l)
  78.         b+=*(ULONG *)args[2]-1;
  79.         else
  80.         b+=l;
  81.     }else
  82.         if(l>max)
  83.         b+=l-max;
  84.     l=max;
  85.     while(l--&&*b)
  86.         if(FPutC(out,*b++)<0)
  87.         ERROR(RETURN_ERROR);
  88.     a++;
  89.     if(*a)
  90.         if(FPutC(out,' ')<0)
  91.         ERROR(RETURN_ERROR);
  92.     }
  93.     if(!args[1])
  94.     if(FPutC(out,'\n')<0)
  95.         ERROR(RETURN_ERROR);
  96.     if(!Flush(out))
  97.     ERROR(RETURN_ERROR);
  98. end:
  99.     if(args[4])
  100.     Close(out);
  101.     FreeArgs(rda);
  102.     if(error)
  103.     PrintFault(IoErr(),"Echo");
  104.     return error;
  105. }
  106.